package com.vishop.controllers;

import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import com.vishop.models.Categorie;
import com.vishop.models.Merk;
import com.vishop.models.Product;
import com.vishop.services.CategorieService;
import com.vishop.services.MerkService;
import com.vishop.services.ProductService;

@Controller
public class ProductController
{
	@Autowired
	private ProductService productService;
	
	@Autowired
	private MerkService merkService;
	
	@Autowired
	private CategorieService categorieService;
	
	@Autowired
	private ProductFormValidator validator;
	
	@RequestMapping("/zoekProducten")
	public ModelAndView searchProductenBeheer(@RequestParam(required= false, defaultValue="") String naam)
	{
		ModelAndView mav = new ModelAndView("showProducten");
		Collection<Product> producten = productService.zoekProducten(naam.trim());
		mav.addObject("alleProducten", producten);
		return mav;
	}
	
	@RequestMapping("/zoekProduct")
	public ModelAndView searchProductenKlant(@RequestParam(required= false, defaultValue="") String naam)
	{
		ModelAndView mav = new ModelAndView("zoekResultaten");
		Collection<Product> producten = productService.zoekProducten(naam.trim());
		mav.addObject("alleProducten", producten);
		return mav;
	}
	
	@RequestMapping("/heren")
	public ModelAndView getHerenProducten()
	{
		ModelAndView mav = new ModelAndView("heren");
		Collection<Product> producten = productService.getHerenProducten();
		mav.addObject("herenProducten", producten);
		return mav;
	}
	
	@RequestMapping("/dames")
	public ModelAndView getDamesProducten()
	{
		ModelAndView mav = new ModelAndView("dames");
		Collection<Product> producten = productService.getDamesProducten();
		mav.addObject("damesProducten", producten);
		return mav;
	}
	
	@RequestMapping("/alleProducten")
	public ModelAndView getProducten()
	{
		ModelAndView mav = new ModelAndView("showProducten");
		Collection<Product> producten = productService.getAllProduct();
		mav.addObject("alleProducten", producten);
		return mav;
	}
	
	@RequestMapping("/productDetails")
	public ModelAndView getProduct(int id)
	{
		ModelAndView mav = new ModelAndView("showProduct");
		Product product = productService.getById(id);
		mav.addObject("eenProduct", product);
		return mav;
	}
	
	@RequestMapping(value="/saveProduct", method=RequestMethod.GET)
	public ModelAndView newproductForm()
	{
		ModelAndView mav = new ModelAndView("newProduct");
		Product product = new Product();
		Collection<Categorie> categorieen = categorieService.getAll();
		Collection<Merk> merken = merkService.getAll();
		mav.addObject("categorieen", categorieen);
		mav.addObject("merken", merken);
		
		mav.getModelMap().put("newProduct", product);
		return mav;
	}
	
	@RequestMapping(value="/saveProduct", method=RequestMethod.POST)
	public String create(@ModelAttribute("newProduct")Product product, BindingResult result, SessionStatus status)
	{
		if(product.getNaam().isEmpty())
		{
			return "redirect:alleProducten.htm";
		}

		productService.save(product);
		status.setComplete();
		return "redirect:alleProducten.htm";
	}
	
	@RequestMapping(value="/updateProduct", method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam("id")Integer id)
	{
		ModelAndView mav = new ModelAndView("editProduct");
		Product product = productService.getById(id);
		Collection<Categorie> categorieen = categorieService.getAll();
		Collection<Merk> merken = merkService.getAll();
		mav.addObject("categorieen", categorieen);
		mav.addObject("merken", merken);
		
		mav.addObject("editProduct", product);
		return mav;
	}
	
	@RequestMapping(value="/updateProduct", method=RequestMethod.POST)
	public String update(@ModelAttribute("editProduct") Product product, BindingResult result, SessionStatus status)
	{
		if(product.getNaam().isEmpty())
		{
			return "redirect:alleProducten.htm";
		}
		
		System.out.println(product);
		productService.update(product);
		status.setComplete();
		return "redirect:alleProducten.htm";
	}
	
	@RequestMapping("deleteProduct")
	public ModelAndView delete(@RequestParam("id")Integer id)
	{
		ModelAndView mav = new ModelAndView("redirect:alleProducten.htm");
		productService.delete(id);
		return mav;
	}

	
}
